import("lib.nas");

var get_file_lines=func(filename){
    var ret=[];
    var fd=io.open(filename);
    while(var line=io.readln(fd))
        append(ret,line);
    io.close(fd);
    return ret;
}

var sum=func(vec,colname){
    if(size(vec)<1){
        die("error vector");
    }
    var cols=split('\t',vec[0]);
    for(var p=0;p<size(cols);p+=1)
        if(!cmp(colname,cols[p]))
            break;
    if(p>=size(cols)){
        die("cannot find key \""~colname~"\"");
    }
    #println(cols,' ',colname,' col:',p);
    var res=0;
    for(var i=1;i<size(vec);i+=1){
        cols=split('\t',vec[i]);
        var n=num(cols[p]);
        res+=math.isnan(n)?0:n;
    }
    return res;
}

var mean=func(vec,colname){
    var res=sum(vec,colname);
    return res/(size(vec)-1);
}

var DlRlcStats=get_file_lines("../no-op-DlRlcStats.txt");
var UlRlcStats=get_file_lines("../no-op-UlRlcStats.txt");
var DlRsrpSinrStats=get_file_lines("../no-op-DlRsrpSinrStats.txt");
var UlSinrStats=get_file_lines("../no-op-UlSinrStats.txt");
println("Dl avg Throughput: ",sum(DlRlcStats,"RxBytes")*8/1000/50," kbps");
println("Ul avg Throughput: ",sum(UlRlcStats,"RxBytes")*8/1000/50," kbps");
println("Dl avg SINR: ",10*math.lg(mean(DlRsrpSinrStats,"sinr"))," Db");
println("Ul avg SINR: ",10*math.lg(mean(UlSinrStats,"sinrLinear"))," Db");
println("avg RSRP: ",mean(DlRsrpSinrStats,"rsrp"));